using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace LS.GameKit.Editor
{
    public class TransformClip
    {
        public string Path;
        public Transform Instance;
        public TransformClip Parent;
        public List<TransformClip> Children;

        public int ChildrenCount
        {
            get
            {
                if (Children == null) return 0;
                return Children.Count;
            }
        }

        public TransformClip()
        {
            Path = string.Empty;
        }

        public TransformClip(Transform transform)
        {
            Path = transform.name;
            Instance = transform;
            ReadTransform(transform);
        }

        public void ReadTransform(Transform transform)
        {
            Path = transform.name;
            Instance = transform;
            
            if (transform.childCount > 0)
            {
                Children = new List<TransformClip>();
                for (int i = 0; i < transform.childCount; i++)
                {
                    var child = transform.GetChild(i);
                    Children.Add(new TransformClip(child));
                }
            }
        }

        public Transform CreateCopy(Transform parent)
        {
            var newTs = new GameObject(Path).transform;
            if (parent != null) newTs.transform.SetParent(parent);
            CopyTransformTo(newTs);
            return newTs;
        }

        public Transform CreateCopy(Transform parent,Type[] keepComponentTypes,bool createKeepTypesOnly)
        {
            //TODO: 实现拷贝组件参数的逻辑
            var newTs = new GameObject(Path).transform;
            if (parent != null) newTs.transform.SetParent(parent);
            CopyTransformTo(newTs);
            return newTs;
        }

        public void CopyTransformTo(Transform newTs)
        {
            newTs.localRotation = Instance.localRotation;
            newTs.localPosition = Instance.localPosition;
            newTs.localScale = Instance.localScale;
        }
    }
}
